#include "includes.h"


void SPI_MasterInit(void)
{
	/* Set MOSI(PB2), SS(PB0) and SCK(PB1) output, all others input */
	DDRB = (1<<DDB2)|(1<<DDB0)|(1<<DDB1);
	
	// Sets the SS inactive
	disable_spi_transmission(MCP); 

	/* Enable SPI, Master, set clock rate fck/128 */
	SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<SPR1);
}


void enable_spi_transmission(uint8_t slave)
{
	clear_bit(PORTB, slave); 
}


void disable_spi_transmission(uint8_t slave)
{
	set_bit(PORTB, slave);
}


uint8_t SPI_read()
{
	return (uint8_t) SPDR;
}


void SPI_write(char cData)
{
	/* Start transmission */
	SPDR = cData;
	/* Wait for transmission complete */
	while(!(SPSR & (1<<SPIF)))
	;
}
